//
//  ShopCartBottomView.m
//  FD
//
//  Created by Yasin on 2018/10/15.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  购物车view底部view
//11:00 页面完成 button、uilabe 文字都始终在底部。 不要再一个问题上耗费太多时间。搞不定 就别耗时间了。、
//11:50 点击底部可以弹出一个tableView
//写界面写熟看，写的快，你的效率就高
//高度固定为 100； ---》 80
// 日了狗写的恶心
#import "ShopCartBottomView.h"
@interface ShopCartBottomView()
/**1.购物车按钮btn*/
@property (nonatomic, strong) UIButton *cartBtn;
/**2.红色的角标 暂时隐藏 10-20 */
@property (nonatomic, strong) UILabel *badgeLb;
/**3.价格lb 使用富文本*/
@property (nonatomic, strong) UILabel *priceLb;
/**原价*/
@property (nonatomic,  strong) UILabel *originPriceLb;
/**配送时间view*/
@property (nonatomic,  strong) UILabel *timeLb;


/**5.备注："另需配送费10元 | 支持自提
 12-31 修改 文字为 VIEW BASKET.
 */
@property (nonatomic, strong) UILabel *remarkLb;
/**6.去结算 按钮 11-27修改,请求到运费后:红色,可以点击; 反之灰色、不然点击*/
@property (nonatomic, strong) UIButton *jieSuanBtn;
//@property (nonatomic, strong) UILabel *jieSuanLb;

@end

@implementation ShopCartBottomView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.cartBtn];
//    [self addSubview:self.badgeLb];
    [self addSubview:self.priceLb];
    [self addSubview:self.remarkLb];
    [self addSubview:self.originPriceLb];
    self.originPriceLb.hidden = YES;
    [self addSubview:self.timeLb];
    [self addSubview:self.jieSuanBtn];
//    [self addSubview:self.jieSuanLb];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //购物车按钮
    [self.cartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.left.equalTo(self.mas_left).offset(10);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartBtn.mas_right).offset(5);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(20);
        make.width.greaterThanOrEqualTo(@40);
    }];
    
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLb.mas_right).offset(5);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(20);
    }];
    
    [self.remarkLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cartBtn.mas_right).offset(5);
        make.top.equalTo(self.priceLb.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    [self.jieSuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.top.bottom.right.equalTo(self);
    }];
    //12-31 左图右文字
    [self.jieSuanBtn buttonWithTextImg];
//    [self.jieSuanLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(110);
//        make.top.bottom.right.equalTo(self);
//    }];
    
    //按钮 处置居中
//    [self.jieSuanBtn verticalCenterImageAndTitleWithSpacing:20];
}

#pragma mark - getter --- ↓↓↓↓↓↓↓↓↓
- (UIButton *)cartBtn {
    if (!_cartBtn) {
        _cartBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cartBtn setImage:[UIImage imageNamed:@"gwc.png"] forState:(UIControlStateNormal)];
        [_cartBtn addTarget:self action:@selector(clickCartBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cartBtn;
}

//圆形角标:(30,30)
- (UILabel *)badgeLb {
    if (!_badgeLb) {
        _badgeLb = [UILabel new];
        _badgeLb.textColor = [UIColor whiteColor];
        _badgeLb.backgroundColor = [UIColor redColor];
        _badgeLb.layer.masksToBounds = YES;
        _badgeLb.layer.cornerRadius = 10;
        _badgeLb.font = [UIFont systemFontOfSize:12.0];
        _badgeLb.text = [NSString stringWithFormat:@"5"];
        _badgeLb.textAlignment = NSTextAlignmentCenter;
    }
    return _badgeLb;
}
- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [UILabel new];
        _priceLb.text = @"€ 0.0";
        _priceLb.font = [UIFont systemFontOfSize:16.0*kScaleFit];
        _priceLb.textColor = [UIColor redColor];
    }
    return _priceLb;
}

- (UILabel *)remarkLb {
    if (!_remarkLb) {
        _remarkLb = [UILabel new];
        //国际化
//        if ([NSString rb_ChineseLanguage]) {
//            _remarkLb.text = @"另需配送费:€ 0.0|支持自提";
//        }else {
//            _remarkLb.text = @"Freight:€ 0.0|collection";
//        }
        
        //12-31
        _remarkLb.text = @"VIEW BASKET"; //2019-2-22 不该有句号
        //参考android
        _remarkLb.font = [UIFont systemFontOfSize:15.0];
    }
    return _remarkLb;
}

- (UIButton *)jieSuanBtn {
    if (!_jieSuanBtn) {
        _jieSuanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_jieSuanBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        NSString *qjsStr = NSLocalizedString(Order_Settlement, @"去结算");
        
        [_jieSuanBtn setTitle:[NSString stringWithFormat:@"%@",qjsStr] forState:(UIControlStateNormal)];
//        _jieSuanBtn.titleLabel.backgroundColor = [UIColor brownColor];
        _jieSuanBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        _jieSuanBtn.backgroundColor = kMainRedColor;
        [_jieSuanBtn setImage:[UIImage imageNamed:@"leftArrow"] forState:(UIControlStateNormal)];
        [_jieSuanBtn addTarget:self action:@selector(clickJieSuanBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _jieSuanBtn;
}

- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
        _originPriceLb.text = @"€ 0.0";
        _originPriceLb.textColor = rgba(143, 143, 143, 1.0);
        _originPriceLb.font = [UIFont systemFontOfSize:15.0];
    }
    return _originPriceLb;
}

- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
        _timeLb.backgroundColor = rgba(170, 170, 170, 1.0);
        _timeLb.textColor = [UIColor whiteColor];
        _timeLb.font = [UIFont systemFontOfSize:10.0*kScaleFit];
        _timeLb.layer.cornerRadius = 5;
        _timeLb.text = @"10:0am~11:0pm";
        _timeLb.layer.masksToBounds = YES;
    }
    return _timeLb;
}


#pragma mark - 事件监听
/**点击购物车按钮*/
- (void)clickCartBtnAction:(UIButton *)btn {
    NSLog(@"点击购物车按钮 %s--%d",__FUNCTION__,__LINE__);
    
    if ([self.delegate respondsToSelector:@selector(shopcartViewDidClickCartBtn:)]) {
        [self.delegate shopcartViewDidClickCartBtn:btn];
    }
}
/**去结算*/
- (void)clickJieSuanBtnAction:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(shopcartViewDidClickJieSuanBtnAction:)]) {
        [self.delegate shopcartViewDidClickJieSuanBtnAction:btn];
    }
}

/**3.更新总价*/
- (void)updateshopcartBottomViewTotalPrice:(CGFloat)totalPrice {
    NSString *priceStr = [NSString stringWithFormat:@"€ %.1f",totalPrice];
    self.priceLb.text = priceStr;
    
//    //12-31 计算文字长度
//    CGSize size = [priceStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
//    
//    //更新约束
//    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.cartBtn.mas_right).offset(5);
//        make.top.equalTo(self.mas_top).offset(5);
//        make.size.mas_equalTo(CGSizeMake(size.width, 20));
//    }];
    
//    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.cartBtn.mas_right).offset(5);
//        make.top.equalTo(self.mas_top).offset(5);
//        make.size.mas_equalTo(CGSizeMake(60, 20));
//    }];
}

/**4.更新营业时间*/
- (void)updateYingYeShiJianBegin:(NSString *)begin end:(NSString *)end{
    
    //背景颜色红，文字颜色白
    self.timeLb.textColor = [UIColor whiteColor];
    self.timeLb.backgroundColor = kMainRedColor;
    
    self.timeLb.text = [NSString stringWithFormat:@"%@am~%@pm",begin,end];
}

/**5.更新运费 运费价格计算接口请求比较慢，客户端处理为：
 如果接口请求中，运费显示“计算中...”，
 同时确认订单按钮为灰色不可点击状态，接口请求完毕后恢复正常*/
- (void)updateFreightAndUpdateJieSuanBtnStateWithFreight:(CGFloat)freight {

    if (freight) { //请求到运费就显示 & 按钮可以点击
//        国际化
        
//        12-31 修改为 VIEW BASKET.
//        if ([NSString rb_ChineseLanguage]) {
//            _remarkLb.text = [NSString stringWithFormat:@"另需配送费€%.1f|支持自提",freight];
//        }else {
//            _remarkLb.text = [NSString stringWithFormat:@"Freight€%.1f|collection",freight];
//        }
        
        //按钮可点击,红色
        [self updateBtnBgcEnable:YES];
    }else { //没有请求到运费。按钮不让点击
        //按钮不可点击,灰色
        [self updateBtnBgcEnable:NO];
    }
}

/**5-2为了应付上一个需求*/
- (void)updateBtnBgcEnable:(BOOL)enable {
    if (enable) {
        self.jieSuanBtn.backgroundColor = kMainRedColor; //灰色的
        self.jieSuanBtn.enabled = YES;//不让点
    }else {
        self.jieSuanBtn.backgroundColor = [UIColor lightGrayColor]; //灰色的
        self.jieSuanBtn.enabled = NO;//不让点
    }
}
@end
